<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header flex justify-between">
        <el-button class="button" @click="addSortList" type="primary">新增优惠卷</el-button>
        <el-icon>
          <Loading />
        </el-icon>

      </div>
    </template>
    <el-table :data="SortList" style="width: 100%">
      <el-table-column label="优惠卷名称" width="280">
        <template #default="scope">
          <div :class="scope.row.is_check?'dashed_red':'dashed'">
            <span>{{ scope.row.name }}</span>
            <div>
              <span>{{ scope.row.timer.slice(2,scope.row.timer.search(',')-1)}}</span>~
              <span>{{ scope.row.timer.slice(scope.row.timer.search(',')+2,scope.row.timer.lastIndexOf(']')-1)}}</span>

            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="130">
        <template #default="scope">
          {{ scope.row.is_check?'领取中':'已过期' }}
        </template>
      </el-table-column>
      <el-table-column label="优惠" width="130">
        <template #default="scope">
         优惠:{{ scope.row.price }}元
        </template>
      </el-table-column>
      <el-table-column label="发放数量" width="130" >
      <template #default="scope">
      {{ scope.row.number }}
      </template>
</el-table-column>

<el-table-column label="已使用" width="130" >
      <template #default="scope">
      {{ scope.row.haveuser }}
      </template>
</el-table-column>
<el-table-column label="操作" >
      <template #default="scope">
      <el-tag :type="scope.row.is_check?'danger':'success'" @click="operation(scope.row.id,scope.row.is_check)">
        {{ scope.row.is_check?'失效':'删除' }}
      </el-tag>
      </template>
</el-table-column>
    </el-table>
  </el-card>
  <Drawer>
    <template v-slot:title>
      <div>{{ titleSort }}</div>
    </template>
    <template v-slot:conten>
      <el-form>
        <el-form-item label="优惠卷名">
          <el-input v-model="titlepreviso"></el-input>
        </el-form-item>
        <el-form-item label="优惠卷数量">
          <el-input-number v-model="value1" :min="1" :max="9999" @change="handleChange" />
        </el-form-item>
        <el-form-item label="优惠价格">
          <el-input-number v-model="value2" :min="1" :max="1000" @change="handleChange" />
        </el-form-item>
        <el-form-item label="活动时间">
          <el-config-provider :locale="locale">
            <el-date-picker v-model="value3" type="daterange" placeholder="选择日期" format="YYYY/MM/DD"
              value-format="YYYY-MM-DD" start-placeholder="Start date" end-placeholder="End date" :locale="locale" />
          </el-config-provider>
        </el-form-item>
      </el-form>
    </template>
    <template v-slot:footer>
      <el-button @click="cancelClick">取消</el-button>
      <el-button type="primary" @click="confirmClick">确认</el-button>
    </template>
  </Drawer>
</template>
  
<script setup>
import { ElConfigProvider,ElMessage, ElMessageBox  } from 'element-plus'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import { reactive, ref, toRaw } from 'vue';
import { useStore } from 'vuex' // 引入useStore 方法
import { getPreferential, addPreferential, delPreferential, updatePreferential } from "../API/index"
import Drawer from "../components/NewDrawer.vue";
const store = useStore()  // 该方法用于返回store 实例
const value1 = ref(1)
const value2 = ref(1)
const value3 = ref('')
const locale = zhCn
const titlepreviso = ref('')
// 所有数据集合
let SortList = reactive([])

// 获取所有分类数据
let getList = async () => {
  SortList.length = 0
  let { data } = await getPreferential()
  SortList.push(...data)
  console.log(data);
}
getList()
let titleSort = ref('')
// 添加
const addSortList = () => {
  titleSort.value = '添加'
  value1.value = 1
  store.commit('DRAWERBOL', true);
}
let Sortids = ref('')
const confirmClick = async () => {
  console.log(toRaw(value3._value));

      let datas = await addPreferential({ price: value2._value,number:value1._value, name: titlepreviso._value, timer: JSON.stringify(toRaw(value3._value))})
        console.log(datas);
      ElMessage({
        type: 'success',
        message: '添加成功'
      }) 
  getList()
  store.commit('DRAWERBOL', false);

}
// 删除
const operation = async (idss,names) => {
  console.log(idss,names);
  if(!names){
  let data = await delPreferential({ id: idss })
  ElMessage({
    type: 'success',
    message: '删除成功'
  })
  getList()

  }else{
    ElMessageBox.confirm(
    '优惠卷改为失效',
    'Warning',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async() => {
    let data =await updatePreferential({ id: idss, is_check: 0 })
      ElMessage({
        type: 'success',
        message: '修改成功',
      })
  getList()

    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '修改失败',
      })
    })
  }
}
const cancelClick = () => {
  store.commit('DRAWERBOL', false);

}
</script>

<style scoped>
.dashed {
  border: 2px dashed var(--el-border-color);
}
.dashed_red {
  --tw-bg-opacity: 1;
    background-color: rgba(255,241,242,var(--tw-bg-opacity));
    --tw-border-opacity: 1;
    border-color: rgba(254,205,211,var(--tw-border-opacity));
    --tw-text-opacity: 1;
    color: rgba(248,113,113,var(--tw-text-opacity));
}
</style>